import store from '@/common/store/index.js'
const request = (params) => {
	return new Promise((resolve, reject) => {
		let header = params.header || {}
		if (!header.notToken) {
			header.Authorization = store.state.token
		}
		uni.request({
			url: params.url,
			method: params.method || 'GET',
			header: header,
			data: params.data || {},
			// dataType: 'json',
			dataType: 'string', //1.先将dataType设置为string
			success: res => {
				let json = res.data.replace(/:s*([0-9]{15,})s*(,?)/g, ': "$1" $2')
				//2.根据后端返回的数据调用一次或者两次replace替换
				let json1 = json.replace(/:s*([0-9]{15,})s*(,?)/g, ': "$1" $2')
				//3.手动转换回json数据即可
				let trueData = JSON.parse(json1);
				// console.log(trueData, "trueData==========================")
				if (trueData.statusCode === 500) {
					uni.showToast({
						icon: 'none',
						title: '服务器维护中'
					})
					return
				}
				if (trueData.code === 401) {
					uni.showToast({
						icon: 'none',
						title: '请登录'
					})
					// 是否登陆
					store.commit('user/setIsLogin', false)
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return
				}
				if (trueData.code === 500) {
					if (trueData.msg && trueData.message) {
						uni.showToast({
							icon: 'none',
							title: trueData.message
						})
					}
					if (trueData.msg) {
						uni.showToast({
							icon: 'none',
							title: trueData.msg
						})
					}
					if (trueData.message) {
						uni.showToast({
							icon: 'none',
							title: trueData.message
						})
					}
					resolve(trueData)
					return
				}
				if (trueData.code === 555) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					setTimeout(() => {
						uni.showToast({
							icon: 'none',
							title: '请登录',
						})
					}, 500)
					return
				}
				console.log("trueData", trueData)
				resolve(trueData)
			},
			fail: error => {
				console.log("error", error)
				uni.showToast({
					icon: 'none',
					title: '当前网络信号不好，请稍后再试'
				})
				reject(error)
			}
		})
	})
}

export default request